package com.tianqu.tms.www.permission.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.taglib.MenuBean;
import com.tianqu.tms.core.taglib.MenuTag;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.UserFunctionDto;
import com.tianqu.tms.entity.UserClass;
import com.tianqu.tms.entity.UserRole;
import com.tianqu.tms.www.permission.viewhelper.PermissionManageViewHelper;

/**
 * @author zhangxueliang 权限修改详细
 */
public class PermissionUpdateDetailController extends TmsController {
	private Logger logger = Logger
			.getLogger(PermissionUpdateDetailController.class);

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			int role = (Integer) uc.getAttribute("role");
			UserRole userRole = UserRole.getInstance();
			userRole = userRole.findRoleByRole(role);
			PermissionManageViewHelper viewhelper = new PermissionManageViewHelper();
			viewhelper.setRoleStatus(userRole.getRoleStatus());
			viewhelper.setRoleName(userRole.getRoleName());
			UserClass userClass = UserClass.getInstance();
			viewhelper.setUserClassName(userClass
					.getUserClassNameByUserClass(userRole.getUcId()));
			// 载入class列表
			List userClassList = new ArrayList();
			userClassList = userClass.getUserClassList();
			viewhelper.setUserClassList(userClassList);
			viewhelper.setOleRole(role);
			viewhelper.setUserClassId(userRole.getUcId());

			String funcId = userRole.getFuncIds();
			MenuBean bean = uc.getRootMenuBean();
			List<MenuBean> functionList = new ArrayList<MenuBean>();
			functionList = 	bean.getChildrenList();
			List codesList = new ArrayList();
			int k =1;
			int total = 0;
			for(MenuBean menuBean : functionList){
				if(menuBean != null){
					if(menuBean.getLevel() == 1){
						UserFunctionDto dto = new UserFunctionDto();
						dto.setFuncId(menuBean.getFunctionId());
						dto.setFuncName(menuBean.getMenuName());
						dto.setFuncGrade(1);//父
						k=1;
						if(funcId.indexOf(menuBean.getFunctionId()) > -1){
							total = 0;
							String mFuncId = menuBean.getFunctionId().substring(0,1);
						
							for (String tmp = funcId; tmp != null&&tmp.length()>=mFuncId.length();){
							  if(tmp.indexOf(mFuncId) == 0){
							    total ++;
							  }
							  tmp = tmp.substring(1);
							}
							dto.setChecked(1);
							codesList.add(dto);
						}
					
						List<MenuBean> menuBeanCList = menuBean.getChildrenList();
						
						if(menuBeanCList.size() > 0 && menuBeanCList != null){
							for(MenuBean menuBeanC : menuBeanCList){
								UserFunctionDto dtoC = new UserFunctionDto();
								dtoC.setFuncId(menuBeanC.getFunctionId());
								dtoC.setFuncName(menuBeanC.getMenuName());
								dtoC.setFuncGrade(2);//子								
								if(funcId.indexOf(menuBeanC.getFunctionId()) > -1){
									if(k % 2 ==0){
										dtoC.setTrFlag(2);
									}else{
										dtoC.setTrFlag(1);
									}
									if(k == total - 1){
										if(k % 2 ==0){
											dtoC.setTrFlag(2);
										}else{
											dtoC.setTrFlag(3);
										}
										
									}
									dtoC.setChecked(1);
									codesList.add(dtoC);
									k++;
								}
								
							}
						}
					}
				}
			}

			viewhelper.setUserFunctionList(codesList);

			uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);

			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
